package us.mooresr.webtroopmanager.client.activity;

import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.events.EventAddedEvent;
import us.mooresr.webtroopmanager.client.place.EditEventPlace;
import us.mooresr.webtroopmanager.client.ui.EditEventView;
import us.mooresr.webtroopmanager.shared.Event;
import us.mooresr.webtroopmanager.shared.EventSummary;
import us.mooresr.webtroopmanager.shared.PersonSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EditEventActivity extends MyActivity implements EditEventView.Presenter {

	public EditEventActivity(EditEventPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
	}

	@Override
	public void addEvent(Event e) {
	TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		
		AsyncCallback<EventSummary> callback = new AsyncCallback<EventSummary>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(EventSummary result) {
				
				clientFactory.getEventBus().fireEvent(new EventAddedEvent(result));

			}
		};
		
		troopManagerService.addEvent(e, callback);
		
		
	}


	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		EditEventView editEventView = clientFactory.getEditEventView();
		editEventView.setPresenter(this);
		getEvent(((EditEventPlace)place).getEventId());
		populateComboBoxes();
		panel.setWidget(editEventView.asWidget());

		
	}
	
	private void getEvent(Long eventId) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		if (eventId == null) {
			clientFactory.getEditEventView().setData(null);
			return;
		}
		
		AsyncCallback<Event> callback = new AsyncCallback<Event>() {
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getEditEventView().setData(null);
				
			}
			
			@Override
			public void onSuccess(Event result) {
				clientFactory.getEditEventView().setData(result);
				
			}
		};
		
		troopManagerService.getEvent(eventId, callback);

	}
	
	private void populateComboBoxes() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);

		AsyncCallback<List<PersonSummary>> callback = new AsyncCallback<List<PersonSummary>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<PersonSummary> result) {
				clientFactory.getEditEventView().setComboBoxes(result);
			}
		};

		troopManagerService.getAllAdults(callback);
	}
	
}
